package com.kpl.kplstzble.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.kpl.kplstzble.R;
import com.kpl.kplstzble.entity.HistoryData;

import java.util.HashMap;
import java.util.List;

public class DataAdapter extends BaseExpandableListAdapter {
    //组
    private List<String> gGroups ;
    //组统计
    private  HashMap<String, Double> everydaydataHashMap ;
    //子项
    private List<List<HistoryData>> iItems;
    //上下文
    private Context mContext;
    //在适配器中定义  自定义的click方法：  ----继承自你自定义的接口中

   // private ClickListener mClickListener;
    //构造器
    public DataAdapter(Context mContext, List<String> gGroups, HashMap<String, Double> everydaydataHashMap, List<List<HistoryData>> iItems/*, ClickListener clickListener*/ ){

        this.mContext = mContext;
        this.gGroups = gGroups;
        this.everydaydataHashMap=everydaydataHashMap;
        this.iItems = iItems;
        //this.mClickListener = clickListener;

    }
   // 可以选择自定义接口------...表示多个参数

    public interface ClickListener {
        public void onClick(Object ... objects);
    }
    @Override
    public int getGroupCount() {
        //获取所有分组个数
        return this.gGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        //获取 指定 (groupPosition)分组的  子项 个数
        //因为 iItems 是由一个ArrayList<ArrayList<Item>> 就相当于  二维数组  [] []
        //[0] [0]
        //[1] [1]
        //所以就一一对应 当我们需要找 第一个组里面的 子项的时候
        //第一个组 下标就是  0   然后 我们就可以获取iItem 在 0,0 位置的一共由多少个个数
        return iItems.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        //获取指定(groupPosition) 组 对象

        return gGroups.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        //获取指定(groupPosition)分组的 (childPosition)子项 对象

        return iItems.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        //获取 指定 组 Id
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        //获取 指定 (groupPosition)组的指定 (childPosition)子项 Id
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //如果返回true,表示子项和组的ID始终表示一个固定的组件对象
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        //获取指定(groupPosition)组的 view 组件
        ViewHolderGroup groupHolder ;

        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_group, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
            groupHolder.tv_everydaydata = (TextView) convertView.findViewById(R.id.tv_everydaydata);

            convertView.setTag(groupHolder);
        }else{
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.tv_group_name.setText(gGroups.get(groupPosition));
        groupHolder.tv_everydaydata.setText(everydaydataHashMap.get(gGroups.get(groupPosition))+"亩");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        //获得指定(groupPosition)组中的(childPosition)子项的 view 组件
        ViewHolderItem itemHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_exlist_item,parent,false);
            itemHolder = new ViewHolderItem();
            itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_bookName);
            itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.image_icon);
            itemHolder.tv_bookPrice =(TextView) convertView.findViewById(R.id.tv_bookPrice);
           // itemHolder.img_datadetail = (ImageView) convertView.findViewById(R.id.image_datadetail);
           /* itemHolder.img_datadetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    System.out.println(groupPosition+":"+childPosition);
                    mClickListener.onClick(iItems.get(groupPosition).get(childPosition));

                }
            });*/
            convertView.setTag(itemHolder);

        }else{
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        itemHolder.img_icon.setImageResource(R.drawable.mapchange);
        itemHolder.tv_name.setText(iItems.get(groupPosition).get(childPosition).getDate());
        itemHolder.tv_bookPrice.setText(iItems.get(groupPosition).get(childPosition).getMunum() + "亩");
       // itemHolder.img_datadetail.setImageResource(R.drawable.mac);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //项是否可以被选中
        return true;
    }

    private static class ViewHolderGroup{
        private TextView tv_group_name;
        private TextView tv_everydaydata;
    }

    private static class ViewHolderItem{
        private ImageView img_icon;
        private TextView tv_name;
        private TextView tv_bookPrice;
       // private ImageView img_datadetail;

    }
}

